package cn.fsw.pethome.org.controller;

import cn.fsw.pethome.org.domain.Detail;
import cn.fsw.pethome.org.query.DetailQuery;
import cn.fsw.pethome.org.service.IDetailService;
import cn.fsw.pethome.org.util.AjaxResult;
import cn.fsw.pethome.org.util.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
@Api(value = "系统词典的API",description="词典详情相关的CRUD功能")
public class DetailController {
    @Autowired
    private IDetailService detailService;

    @GetMapping("/list")
    @ApiOperation(value = "查询全部系统词典信息" )
    public List<Detail> selectAll(){
        return detailService.selectAll();
    }

    @PostMapping("/pageList")
    @ApiOperation(value = "高级查询和分页" )
    public PageList<Detail> pageSelevtAll(@RequestBody DetailQuery query){

        return detailService.pageSelevtAll(query);
    }

    @GetMapping("/list/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Detail selectById(@PathVariable("id") Long id){
        return detailService.selectById(id);
    }


    @PutMapping("/detail")
    @ApiOperation(value = "新增或者修改系统词典信息" )
    public AjaxResult update(@RequestBody Detail detail){
        try {
            if(detail.getId() == null){
                detailService.add(detail);
            }else {
                detailService.update(detail);
            }
            return AjaxResult.ajaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @DeleteMapping("/detail/{id}")
    @ApiOperation(value = "通过ID删除" )
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            detailService.delete(id);
            return AjaxResult.ajaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
}
